#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite3.h> 

#include <fstream>
#include <iostream>
#include <string>

static int callback(void *data, int argc, char **argv, char **azColName){
  int i;
  for(i=0; i<argc; i++){
    printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  }
  printf("\n");
  return 0;
}

int main(int argc, const char* argv[])
{
   sqlite3 *db;
   char *zErrMsg = 0;
   int rc;
   char sql[65535];
   const char *dbName = "users.db";
   int isGet = 0;

   if(argc < 2)
     return 0;

   //notice it is fixed and hard coded
   if(strcmp(argv[1],"get") == 0)
     {
       isGet = 1;
       sprintf(sql, "SELECT * FROM %s", argv[2]);
     }
   else if (strcmp(argv[1], "add") == 0)
     {
       if(strcmp(argv[2], "game") == 0)
	 sprintf(sql, "INSERT INTO games (name) VALUES ('%s')",argv[3]);
       else if(strcmp(argv[2], "category") == 0)
	 {
	   char imgLoca[255];
	   char level[7];

	   //32 = space
	   if(argv[4][0] == 32)
	     strcpy(imgLoca,"NULL");
	   else
	     strcpy(imgLoca,argv[4]);

	   if(argv[5][0] == 32)
	     strcpy(level,"NULL");
	   else
	     strcpy(level,argv[5]);

	   sprintf(sql,
		   "CREATE TABLE %s("
		   "word_id INTEGER PRIMARY KEY AUTOINCREMENT,"
		   " level int NOT NULL,"
		   " swahili text NOT NULL,"
		   " english text NOT NULL);"
		   "INSERT INTO categories (name, imgLocation, level) values ('%s','%s','%s');",
		   argv[3],argv[3],imgLoca,level);
	 }
       else if(strcmp(argv[2], "words") == 0)
	 {
	   std::string line;
	   int x = 1;
	   std::string english;
	   std::string swahili;
	   std::string sqlString = "";
	   std::ifstream myfile(argv[5]);

	   if(myfile.is_open())
	     {
	       while(std::getline(myfile, line))
		 {
		   x = 0;
		   for(char& c : line)
		     {
		       if( c == 32 ) break;
		       ++x;
		     }
		   
		   english = line.substr(0,x);
		   swahili = line.substr(x+1,line.size()-x);
		   sprintf(sql, "insert into %s (english,swahili,level) values ( '%s', '%s', '%s');",argv[3],english.c_str(),swahili.c_str(),argv[4]);
		   sqlString.append(sql);
		 }
	       
	       strcpy(sql, sqlString.c_str());
	       myfile.close();
	     }
	   else
	     {
	       printf("Unable to open file: %s\n", argv[5]);
	     }	       
	 }
       else if(strcmp(argv[2], "help") == 0)
	 printf("add game (name)\n"
		"add category (name) (imgLocation) (level)\n"
		"add words (category) (level) (filename)\n"
		"add help\n");
       else
	 printf("add command not found");
     }
   else if(strcmp(argv[1], "delete") == 0)
     { 
       if(strcmp(argv[2], "game") == 0)
	 sprintf(sql, "DELETE FROM games WHERE name='%s'",argv[3]);
       else if(strcmp(argv[2], "category") == 0)
	 sprintf(sql,
		 "DELETE FROM categories WHERE name='%s';"
		 "DROP TABLE %s",
		 argv[3],argv[3]);
       else if(strcmp(argv[2], "help") == 0)
	 printf("delete game (name)\n"
		"delete category (name)\n"
		"delete help\n");
       else
	 printf("delete command not found\n");
     }
   else if(strcmp(argv[1], "help") == 0)
     printf("get [table]\n"
	    "add [command]\n"
	    "delete [command]\n"
	    "help\n");
   else
     printf("command not found\n");

   rc = sqlite3_open(dbName, &db);
   if( rc )
     {
       fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
     }
   
   /* Execute SQL statement */
   if(isGet == 1)
     rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
   else 
     rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
   
   if( rc != SQLITE_OK )
     {
       fprintf(stderr, "SQL error: %s\n", zErrMsg);
       sqlite3_free(zErrMsg);
     }
   
   sqlite3_close(db);
   return 0;
}
